<?php
include('config.php');
$type_name = isset($_POST['type']) ? $_POST['type'] : '';
$name_name = isset($_POST['name']) ? $_POST['name'] : '';
$number_name = isset($_POST['number']) ? $_POST['number'] : '';
$guest_name = isset($_POST['guest']) ? $_POST['guest'] : '';
$weight = isset($_POST['weight']) ? $_POST['weight'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$time = isset($_POST['time']) ? $_POST['time'] : '';
$time = time();
$rs = $mysql->selectList('number',"number_name='{$number_name}'");
$number_id = $rs[0]['number_id'];
$where = "number_id={$number_id}";
$re = $mysql->selectList('stock',$where);
$resul = $mysql->selectList('guest',"guest_name='{$guest_name}'");
$guest_id = $resul[0]['guest_id'];

$data = array('type_id'=>$rs[0]['type_id'],'breed_id'=>$rs[0]['breed_id'],'number_id'=>$number_id,'buy_goods_num'=>$weight,'buy_goods_price'=>$price,'guest_id'=>$guest_id,'buy_goods_date'=>$time);


$num = $re[0]['stock_num'];//获取商品库存数量
$buy_number = $re[0]['buy_goods_num']; //获取商品售出数量

if($re && $num>=$weight){
	$bid = $mysql->insert('buy_goods_log',$data); //写入出售信息
	$id = $re[0]['stock_id']; //获取商品的库存id
	$total_buy_number = $buy_number + $weight;
	$stock_weight = $num - $weight;
	$data = array('stock_num'=>$stock_weight,'buy_goods_num'=>$total_buy_number);
	$where = "stock_id={$id}";
	$res = $mysql->update('stock', $where, $data);

	if($res && $bid){
		echo "1";
	}else{
		echo "-1";
	}
}


